/** 文件工具类 */
export class FileUtil {
  //下载.
  static download(fileUrl: string, fileName: string) {
    // 创建隐藏的可下载链接
    const element = document.createElement("a");
    element.setAttribute("href", fileUrl);
    element.setAttribute("download", fileName);

    // 触发点击
    document.body.appendChild(element);
    element.click();

    // 然后移除
    document.body.removeChild(element);
  }
}

// export function getFileIcon(file: any) {
//   const fileIcons: any = {
//     image: "@/assets/images/file_image.png",
//     zip: "@/assets/images/file_zip.png",
//     doc: "@/assets/images/file_word.png",
//     docx: "@/assets/images/file_word.png",
//     pdf: "@/assets/images/file_pdf.png",
//   };

//   // 优先使用fileType
//   if (file.fileType && fileIcons[file.fileType]) {
//     return fileIcons[file.fileType];
//   }
//   // 其次使用fileExt
//   if (file.fileExt && fileIcons[file.fileExt]) {
//     return fileIcons[file.fileExt];
//   }
//   // 默认图标
//   // return "@/assets/images/file_default.png";
// }

// 判断是否有对应的文件图标
export function hasFileIcon(file: any) {
  return (
    file.fileType === "image" ||
    file.fileType === "zip" ||
    ["doc", "docx"].includes(file.fileExt) ||
    file.fileExt === "pdf" ||
    file.fileExt === "xls" ||
    file.fileExt === "xlsx" ||
    file.fileExt === "xlsm" ||
    file.fileExt === "xlsb" ||
    file.fileExt === "xltx" ||
    file.fileExt === "xltm" ||
    file.fileExt === "csv" ||
    file.fileExt === "ppt" ||
    file.fileExt === "pptx" ||
    file.fileExt === "txt"
  );
}

// 获取对应的文件图标路径
export function getFileIcon(file: any) {
  console.log(file);
  const iconMap: any = {
    image: new URL("@/assets/images/file_image.png", import.meta.url).href,
    zip: new URL("@/assets/images/file_zip.png", import.meta.url).href,
    doc: new URL("@/assets/images/file_word.png", import.meta.url).href,
    docx: new URL("@/assets/images/file_word.png", import.meta.url).href,
    pdf: new URL("@/assets/images/file_pdf.png", import.meta.url).href,
    txt: new URL("@/assets/images/file_txt.png", import.meta.url).href,
    ppt: new URL("@/assets/images/file_ppt.png", import.meta.url).href,
    pptx: new URL("@/assets/images/file_ppt.png", import.meta.url).href,
    xls: new URL("@/assets/images/file_excel.png", import.meta.url).href,
    xlsx: new URL("@/assets/images/file_excel.png", import.meta.url).href,
    xlsm: new URL("@/assets/images/file_excel.png", import.meta.url).href,
    xlsb: new URL("@/assets/images/file_excel.png", import.meta.url).href,
    xltx: new URL("@/assets/images/file_excel.png", import.meta.url).href,
    xltm: new URL("@/assets/images/file_excel.png", import.meta.url).href,
    csv: new URL("@/assets/images/file_excel.png", import.meta.url).href,
  };

  return iconMap[file.fileType] || iconMap[file.fileExt];
}
